<?php
class ArticlesController extends AppController {

	var $name = 'Articles';
	
	function beforeFilter(){
		if($this->Session->read('Auth.User')){
			$this->Auth->allow('*');
		}
	}

	function admin_index() {
		$this->Article->recursive = 0;
		$this->set('articles', $this->paginate());
	}

	function admin_add() {
		if (!empty($this->data)) {
			$date = time();
			$this->data['Article']['user_id'] = 1;
			$this->data['Article']['modified_date'] = strftime("%y-%m-%d",$date);
			$this->Article->create();
			if ($this->Article->save($this->data)) {
				$this->Session->setFlash(__('The article has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.', true));
			}
		}
		$categories = $this->Article->Category->find('list');
		$users = $this->Article->User->find('list');
		$this->set(compact('categories', 'users'));
	}
	
	function view($id = NULL, $category = NULL){
		if($category != NULL){
			$this->set(compact('category'));
		}
		$detail = $this->Article->find('first', array('conditions' => array('Article.id' => $id)));
		$this->set('job', $detail);
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid article', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Article->save($this->data)) {
				$this->Session->setFlash(__('The article has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Article->read(null, $id);
		}
		$categories = $this->Article->Category->find('list');
		$users = $this->Article->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function admin_delete($id = null) {
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			if (!$id) {
				return 'error';
			}
			if ($this->Article->delete($id)) {
				return 'Success';
			}
			return 'error';
		}
	}
	
	function getArticles($categoryName = NULL){
		if($categoryName != NULL){
			$contact = $this->Article->Category->find('list', array('conditions' => array('Category.alias' => $categoryName),'fields' => array('id','id')));
			return($this->Article->Find('all', array('conditions' => array('Article.category_id' => $contact,'Article.published' => 1))));
		}else{
			return('');
		}
	}
	
	function tag($value = NULL){
		$this->autoRender = false;
		if($value != NULL){
			$detail = $this->getArticles($value);
			if($detail != ''){
				$this->set('job', $detail[0]);
				$this->set('category',$value);
				$this->render('view');
			}
		}
	}
	
	function getByTagName($value = NULL){
		if($value != NULL){
			$detail = $this->getArticles($value);
			if($detail != ''){
				return($detail[0]);
			}else{
				return('');
			}
		}
	}
	
	function conditions(){
		$menu = false;
		if($this->RequestHandler->isAjax()){
			$this->set('id',$this->params['form']['value']);
			$menu = true;
		}
		$this->set(compact('menu'));
	}
	
	function aboutus(){
		$message = $this->getByTagName('message');
		$about = $this->getByTagName('about');
		$social_responsibility = $this->getByTagName('social-responsibility');
		$mission = $this->getByTagName('mission');
		$branch = $this->getByTagName('branch');
		$this->set(compact('message','about','social_responsibility','mission','branch'));
	}
}
?>